package com.yami.shop.listener;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yami.shop.bean.event.BlockchainEvent;
import com.yami.shop.bean.event.BlockchainShopEvent;
import com.yami.shop.bean.model.BlockchainShop;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.util.DataWebService;
import com.yami.shop.config.ShopConfig;
import com.yami.shop.service.ShopDetailService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lth
 * @Date 2021/8/4 9:27
 */
@Component("Blockchain")
@AllArgsConstructor
@Slf4j
public class BlockchainListener {

    private final ShopDetailService shopDetailService;

    private final ShopConfig shopConfig;

    private final DataWebService dataWebService;

    @EventListener(BlockchainEvent.class)
    @Transactional(rollbackFor = Exception.class)
    public void apply(BlockchainEvent event) {
        Long shopId = event.getShopId();
        Long blockchainId = event.getBlockchainId();
    }

    @EventListener(BlockchainShopEvent.class)
    public void blockchainShopEvent(BlockchainShopEvent event) {
        Long shopId = event.getShopId();
        List<BlockchainShop> lianSigningList = event.getLianSigningList();
        ShopDetail shopDetail = event.getShopDetail();

        if (null == shopId || ObjectUtils.isEmpty(lianSigningList)) {
            return;
        }
        //有系统账号 返回 不回填ApiUser 和 密码
        if(StrUtil.isNotEmpty(shopDetail.getApiUser())){
            return;
        }
        String shopName = shopDetail.getShopName();
        Map<String, Object> stringObjectMap = new HashMap<>();
        stringObjectMap.put("platformCode","dtc");
        stringObjectMap.put("platformName","大头菜");
        stringObjectMap.put("shopId", shopId.toString());
        stringObjectMap.put("channelCode", shopDetail.getChannelCode());
        stringObjectMap.put("shopName", shopName);

        //注册系统账号和签约信息，如果有系统账号只添加签约信息
        String url = shopConfig.getApiGatewayUrl() + "/nft/outapi/nftUser/addUserForRomate";
        JSONObject jsonObject = dataWebService.hutoolPostObjWebService(url, stringObjectMap,null,null);
        if (null == jsonObject || jsonObject.getInteger("code") !=Constant.SUCCESS_CODE  || jsonObject.getJSONObject("data") == null) {
            log.info("商铺" + shopName + "(" + shopId.toString() + ")开通用户失败");
            return;
        }
        JSONObject resObj = jsonObject.getJSONObject("data");
        // 更新店铺属性userPassword
        String apiUser = resObj.getString("userName");
        String apiPassword = resObj.getString("userPassword");
        Long backShopId = resObj.getLong("shopId");
        if (StringUtils.isBlank(apiUser) || StringUtils.isBlank(apiPassword) || backShopId == null) {
            log.info("商铺" + shopName + "(" + shopId.toString() + ")开通用户失败");
            return;
        }
        shopDetailService.update(Wrappers.lambdaUpdate(ShopDetail.class)
                .set(ShopDetail::getApiUser, apiUser).set(ShopDetail::getApiPassword,apiPassword)
                .eq(ShopDetail::getShopId,shopId));
        // 清除缓存
        shopDetailService.removeShopDetailCacheByShopId(shopId);
    }
}
